package by.alex.dzeshko.facebooksuperclient.service;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import by.alex.dzeshko.facebooksuperclient.R;
import by.alex.dzeshko.facebooksuperclient.async.LoadJsonObjectAsyncTask;
import by.alex.dzeshko.facebooksuperclient.async.common.ParamCallback;
import by.alex.dzeshko.facebooksuperclient.http.HttpManager;
import by.alex.dzeshko.facebooksuperclient.providers.contract.FbContract;

public class FeedService extends CommonService<JSONObject> implements ParamCallback<JSONObject>{
	
	private static final int NUMBER = R.string.number;

	private static final int COUNT = R.string.count;

	private static final int DATA = R.string.data;

	public static final String NEXT_PAGE = "by.alex.dzeshko.facebooksuperclient.service.FeedService.NEXT_PAGE";
	
	public static final String NEWS_ON_SUCCESS = "by.alex.dzeshko.facebooksuperclient.service.FeedService.NEWS_ON_SUCCESS";

	public static final String NEWS_ON_ERROR = "by.alex.dzeshko.facebooksuperclient.service.FeedService.NEWS_ON_ERROR";

	public static final String PROFILE_FEED_ON_SUCCESS = "by.alex.dzeshko.facebooksuperclient.service.FeedService.PROFILE_ON_SUCCESS";

	public static final String PROFILE_FEED_ON_ERROR = "by.alex.dzeshko.facebooksuperclient.service.FeedService.PROFILE_ON_ERROR";

	protected static final String LOG_TAG = "FeedService";
	
	@Override
	protected void handleCommand(Intent intent) {
		super.handleCommand(intent);
		new LoadJsonObjectAsyncTask(this).start(requestBuilder);
	}
	
	@Override
	public void onSuccess(JSONObject p) {
		onHandleResponse(p);
	}
	
	@Override
	public void onError(Throwable e) {
		Log.e(LOG_TAG,e.toString());
		Intent intent;
		if(requestBuilder.getDbMarker().equals(FbContract.NEWS)){
			intent= new Intent(NEWS_ON_ERROR);
		} else intent = new Intent(PROFILE_FEED_ON_ERROR);
		intent.putExtra(EXTRA_KEY_MESSAGE, e.toString());
		sendBroadcast(intent);
	}
	
	@Override
	protected void threadedHandleResponse(final JSONObject obj) {
		if(obj.has(getString(ERROR))){
			JSONObject err;
			try {
				err = obj.getJSONObject(getString(ERROR));
				String errMessage = err.getString(getString(TYPE))+err.getString(getString(MESSAGE));
				Log.e(LOG_TAG, err.getString(getString(TYPE))+" "+errMessage);
				onError(new Throwable(errMessage));
				return;
			} catch (JSONException e) {
				onError(e);
			}
		}
		String dbMarker = requestBuilder.getDbMarker();
		try {
			JSONArray array = obj.getJSONArray(getString(DATA));
			List<JSONObject> list = new ArrayList<JSONObject>();
			for (int i = 0; i < array.length(); i++) {
				list.add(array.getJSONObject(i));
			}
			int i=0;
			for (JSONObject jsonObject : list) {
				i++;
				jsonObject.put(FbContract.FeedColumns.TABLE_TYPE, dbMarker);
				ContentValues contentValues = new ContentValues();
				contentValues.put(getString(DATA), jsonObject.toString());
				contentValues.put(getString(COUNT), list.size());
				contentValues.put(getString(NUMBER), i);
				getContentResolver().insert(FbContract.FeedColumns.CONTENT_URI, contentValues);
			}
			Intent intent;
			String nextPage = NO_PAGES;
			if(dbMarker.equals(FbContract.NEWS)){
				intent = new Intent(NEWS_ON_SUCCESS);
			} else intent = new Intent(PROFILE_FEED_ON_SUCCESS);
			
			if(obj.has(getString(PAGING))){
				JSONObject paging = obj.getJSONObject(getString(PAGING));
				if(paging.has(getString(NEXT))){
					nextPage = paging.getString(getString(NEXT));
					Log.d("paging next", nextPage);
				}
			}
			intent.putExtra(NEXT_PAGE, nextPage);
			sendBroadcast(intent);
		} catch (JSONException e) {
			Log.e(LOG_TAG, e.toString());
			onError(e);
		}
	}
}
